package com.hxgis.common.conare;

import com.hxgis.common.entity.RainstormInfo;
import com.hxgis.common.utils.CollectionUtils;
import com.hxgis.common.utils.DistanceUtil;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.*;

/**
 * 国家暴雨组过程
 * @author cqj
 * @version 1.0
 * @date 2019/9/24 16:47
 */
@Slf4j
public class NationArea {

    private String nationFlag;

    private Set<NationGroup> nationGroups = new HashSet<>();


    public NationArea(NationGroup nationGroup) {
        nationGroups.add(nationGroup);
        //生成区域标识
        nationFlag = UUID.randomUUID().toString().replace("-","");
        nationGroup.setNationFlag(nationFlag);
    }



    public void add(NationGroup nationGroup){
        nationGroups.add(nationGroup);
        nationGroup.setNationFlag(nationFlag);
    }

    public void merge(NationArea NationArea){
        nationGroups.addAll(NationArea.getNationGroups());
        setNationFlag(nationFlag);
    }

    private void initAraFlag(){
        for (NationGroup nationGroup : nationGroups) {
            RainstormInfo rainstormInfo = nationGroup.getRainstormInfos().get(0);
            String nationCourse = rainstormInfo.getCourseContinue();
            if (nationCourse != null) nationFlag = nationCourse;
        }
        setNationFlag(nationFlag);
        return;
    }

    public List<RainstormInfo> getRainstormInfoList(){
        initAraFlag();
        List<RainstormInfo> rainstormInfos = new ArrayList<RainstormInfo>();
        for (NationGroup nationGroup : nationGroups) {
            for (RainstormInfo rainstormInfo : nationGroup.getRainstormInfos()) {
                rainstormInfo.setCourseContinue(nationFlag);
                rainstormInfos.add(rainstormInfo);
            }
        }
        return rainstormInfos;
    }

    //进行条件过滤
    public boolean check(NationGroup nationGroup){
        // 计算 时间是否在范围内
        List<NationGroup> nationGroups = checkTime(nationGroup);
        if (!CollectionUtils.isNotBlank(nationGroups)) {
            return false;
        }
        // 计算 两个组之间的中心距离
        return checkArea(nationGroups,nationGroup);
    }

    /**
     * 校验两个组之间的距离
     * @param nationGroups
     * @param nationGroup
     * @return
     */
    private boolean checkArea(List<NationGroup> nationGroups, NationGroup nationGroup) {
        for (NationGroup nationGroup1 : nationGroups) {
            double v = DistanceUtil.countDistance(nationGroup1.getCenterPoint()[1], nationGroup1.getCenterPoint()[0],
                    nationGroup.getCenterPoint()[1], nationGroup.getCenterPoint()[0]);
            if (Double.isInfinite(v) || Double.isNaN(v)){
                log.error("error的double是---{}",v);
                return false;
            }
            BigDecimal bigDecimal = new BigDecimal(v);
            if (bigDecimal.compareTo(new BigDecimal(1050)) <=0){
                return true;
            }
        }
        return false;
    }

    /**
     * 校验时间是否存在交集
     * @param nationGroup
     * @return
     */
    private List<NationGroup> checkTime(NationGroup nationGroup){
        List<NationGroup> nationGroupsList = new ArrayList<NationGroup>();
        for (NationGroup time : nationGroups) {
            if (time.checkTime(nationGroup)){
                nationGroupsList.add(time);
            }
        }
        return nationGroupsList;
    }




    public String getNationFlag() {
        return nationFlag;
    }

    public void setNationFlag(String nationFlag) {
        this.nationFlag = nationFlag;
    }

    public Set<NationGroup> getNationGroups() {
        return nationGroups;
    }

    public void setNationGroups(Set<NationGroup> nationGroups) {
        this.nationGroups = nationGroups;
    }
}
